package com.example.multithread;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalTest {
    public static void main(String[] args) throws InterruptedException {
        Random randoms = new Random();
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (int j = 0; j < 3; j++) {
                    // 线程共享随机因子
                    System.out.printf("【%s】生成随机数，%d%n", Thread.currentThread().getName(), randoms.nextInt(100));
                }
            }, "随机数生成线程" + i).start();
        }
        Thread.sleep(1000);
        System.out.println("=============");
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                for (int j = 0; j < 3; j++) {
                    // 线程独享随机因子
                    System.out.printf("【%s】生成随机数，%d%n", Thread.currentThread().getName(), ThreadLocalRandom.current().nextInt(100));
                }
            }, "随机数生成线程" + i).start();
        }
    }
}
